package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.sczmgk.hotline_pzh.MyApp;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.adapter.MyNewsHomeAdapter;
import com.sczmgk.hotline_pzh.base.RefreshAndLoadMoreBaseActivity;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.entity.InformationBean;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.utils.UIUtils;
import com.sczmgk.hotline_pzh.view.NoDoubleItemClickListener;
import com.sczmgk.hotline_pzh.view.RefreshListView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/6 0006.
 */
public class MoreDetailActivity extends RefreshAndLoadMoreBaseActivity<InformationBean> {

    /*更多详情发送的消息*/
    private static final int MORE_DETAIL_INFO = 100;

    /*传递的模块类型*/
    private int type;

    /*传递过来的头部标题*/
    private String title;

    /*资讯详情的Adapter*/
    private MyNewsHomeAdapter adapter;

    /*搜索框*/
    private LinearLayout mSearchBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_detail);

        setHeader();

        initView();

        initData();

        setListener();

    }

    /**
     * 设置头
     */
    private void setHeader() {

        //获取传递的类型和头部标题
        Intent intent = getIntent();
        type = intent.getIntExtra(Constants.MORE_NEWS_TYPE, 0);
        title = intent.getStringExtra(Constants.MORE_NEWS_TITLE);
        if (!Tools.isEmpty(title)) {
            setTitle(title);
        }
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        //刷新功能的listview
        mListView = (RefreshListView) findViewById(R.id.lv_more_detail);

        //搜索框
        mSearchBar = (LinearLayout) findViewById(R.id.ll_more_search);
    }

    /**
     * 初始化数据
     */
    private void initData() {

        //设置资讯详情的Adapter
        adapter = new MyNewsHomeAdapter(MoreDetailActivity.this);
        mListView.setAdapter(adapter);

    }

    /**
     * 设置监听
     */
    @Override
    public void setListener() {
        super.setListener();
        //更多详情的条目点击事件
        mListView.setOnItemClickListener(new NoDoubleItemClickListener() {

            @Override
            public void NoDoubleItemClickListener(AdapterView<?> parent, View view, int i, long id) {
                int position = i - 1;
                Intent intent = new Intent(MoreDetailActivity.this, NewsDetailActivity.class);
                intent.putExtra(Constants.NEWS_DETAIL_ITEM, lists.get(position));
//                intent.putExtra(Constants.MY_COLLECTION_FLAG, Constants.MY_COLLECTION_FLAG_INFO);
                startActivity(intent);
            }
        });

        //搜索框的点击事件
        mSearchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.startActivity(MoreDetailActivity.this, SearchActivity.class , Constants.SEARCH_MODULE_TITLE , title , "type" , type);
            }
        });

    }

    @Override
    public Map<String, Object> getRequestData(int page, String keyword) {
        Map<String, Object> infos = new HashMap<>();
        //TODO
        infos.put("type", type);
        infos.put("page", page);
        infos.put("imei", MyApp.imei);
        infos.put("userid", CacheUtils.getIntData(MoreDetailActivity.this, Constants.LOGIN_UID, 0));

        return infos;
    }

    @Override
    public String getRquestUrl() {
        return Constants.BASE_URL + Constants.NEWS_MORE_INFORMATION;
    }

    @Override
    public int getListType() {
        return LIST_TYPE_UP_LOAD_MORE;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case REFRESH_LIST:

                adapter.setLists(lists);
                adapter.notifyDataSetChanged();

                break;
        }
    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData(1, "", LOAD_NORMAL);
    }
}
